package com.horoscope.astrology.zodiac.signs.free.adapter.quize

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.activity.QuizActivity
import com.horoscope.astrology.zodiac.signs.free.bean.QuestionBean
import com.horoscope.astrology.zodiac.signs.free.utils.ImageLoader


class QuizTextAdapter(private val mContext: Context) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val mDataList = ArrayList<String>()

    //    private var loadFinished: Boolean = false
    private var mBean = QuestionBean()

    fun setBean(bean: QuestionBean) {
        mBean = bean
        mDataList.addAll(0, bean.answer)
        notifyDataSetChanged()

    }


    override fun onCreateViewHolder(parent: ViewGroup, p1: Int): RecyclerView.ViewHolder {

        return when (p1) {
            HEAD -> {
                val view =
                    LayoutInflater.from(mContext).inflate(R.layout.item_quiz_head, parent, false)
                return HeadViewHolder(view)
            }
            QUESTION -> {
                val view = LayoutInflater.from(mContext)
                    .inflate(R.layout.item_quiz_question, parent, false)
                QuestionViewHolder(view)
            }
            ANSWER -> {
                val view =
                    LayoutInflater.from(mContext).inflate(R.layout.item_quiz_answer, parent, false)
                AnswerViewHolder(view)
            }
            else -> {
                val view =
                    LayoutInflater.from(mContext).inflate(R.layout.item_quiz_answer, parent, false)
                AnswerViewHolder(view)
            }
        }

    }

    override fun getItemCount(): Int {
        return if (mDataList.isEmpty()) {
            0
        } else {
            mDataList.size + 2
        }
    }


    private val array = arrayOf(false, false, false, false)
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, p1: Int) {
        when (holder) {
            is HeadViewHolder -> {

                ImageLoader.loadImageForWidth(holder.title, mBean.imageUrl, 360, 240)

            }
            is QuestionViewHolder -> {

                holder.question.text = mBean.text
            }
            is AnswerViewHolder -> {
                if (p1 - 2 !in array.indices) {
                    return
                }

                if (array[p1 - 2]) {
                    holder.answer.setBackgroundResource(R.drawable.shape_result_ok_999)
                    holder.answer.setTextColor(ContextCompat.getColor(mContext, R.color.white))
                } else {
                    holder.answer.setBackgroundResource(R.drawable.shape_quize_answer_bg)
                    holder.answer.setTextColor(ContextCompat.getColor(mContext, R.color.black_3))
                }
                holder.answer.text = mDataList[p1 - 2]

                holder.itemView.setOnClickListener {
//                    if (SysUtils.isFastClick()) return@setOnClickListener
                    array[p1 - 2] = true
                    for (i in array.indices) {
                        if (i != (p1 - 2)) {
                            array[i] = false
                        }
                    }
                    notifyDataSetChanged()
                    (mContext as QuizActivity).setPage()
                    mContext.setAnswer("${p1 - 2}")
                }
            }
        }

    }


    override fun getItemViewType(position: Int): Int {
        return when (position) {
            0 -> {
                HEAD
            }
            1 -> {
                QUESTION
            }
            else -> {
                ANSWER
            }
        }
    }

    companion object {
        const val HEAD = 0x14
        const val QUESTION = 0x15
        const val ANSWER = 0x16
    }

    class HeadViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val title = itemView.findViewById<ImageView>(R.id.image_view_head)!!
    }

    class QuestionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val question = itemView.findViewById<TextView>(R.id.text_view_question)!!
    }

    class AnswerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val answer: TextView = itemView.findViewById(R.id.text_view_answer)
    }

}